import pygame
import os
from Setting import Location


class InputBox:
    def __init__(self, rect, back_color, text=''):
        self.rect = rect
        self.back_color = back_color
        self.text = text
        self.font = pygame.font.Font(None, 32)
        self.boxBody: pygame.Rect = rect

    def setTxt(self, txt):
        self.text = txt

    # def rendertxt(self, screen):
    #     txtSurface = self.font.render(self.text, True, pygame.Color("black"))
    #     screen.blit(txtSurface, (self.rect.left + 10, self.rect.top + 13))

    def render(self, screen):
        #print("render success")
        txtSurface = self.font.render(self.text, True, pygame.Color("black"))
        pygame.draw.rect(screen, self.back_color, self.rect)
        screen.blit(txtSurface, (self.rect.left + 10, self.rect.top + 13))
        pygame.draw.rect(screen, pygame.Color(128, 128, 128), self.boxBody, 2)


class MineButton:
    def __init__(self, rect, grid_size, out_status=10, in_status=0, flag=True):
        self.rect = rect
        self.grid_size = grid_size
        self.out_status = out_status
        self.in_status = in_status
        self.flag = flag

    def setFlag(self, flag):
        self.flag = flag

    def setInStatus(self, val):
        self.in_status = val

    def setOutStatus(self, val):
        self.out_status = val

    def render(self, screen):
        if self.flag:
            look = pygame.image.load("./icon/" + str(self.out_status) + ".jpg")
            look_1 = pygame.transform.scale(look, (self.grid_size, self.grid_size))
            screen.blit(look_1, (self.rect.left, self.rect.top))
        else:
            look = pygame.image.load("./icon/" + str(self.in_status) + ".jpg")
            look_1 = pygame.transform.scale(look, (self.grid_size, self.grid_size))
            screen.blit(look_1, (self.rect.left, self.rect.top))


class ChessBoardHandler:
    def __init__(self, x, y, width, height, grid_size):
        self.x = x
        self.y = y
        self.width = width
        self.height = height
        self.grid_size = grid_size
        self.grid_board = []

    def render(self, screen):
        for row in range(0, self.height):
            grid_row = []
            for col in range(0, self.width):
                rect = pygame.Rect(self.x + self.grid_size * col, self.y + self.grid_size * row, self.grid_size,
                                   self.grid_size)
                grid = MineButton(rect, self.grid_size)
                grid_row.append(grid)
                grid.render(screen)
            self.grid_board.append(grid_row)

    def turn_render(self, screen, grid_list):
        for grid_row in grid_list:
            if grid_row.flag:
                self.grid_board[grid_row.x][grid_row.y].setOutStatus(grid_row.status)
            else:
                self.grid_board[grid_row.x][grid_row.y].setFlag(grid_row.flag)
                self.grid_board[grid_row.x][grid_row.y].setInStatus(grid_row.status)

            self.grid_board[grid_row.x][grid_row.y].render(screen)

    def cell_render(self, screen, grid_info):

        if grid_info.flag:
            self.grid_board[grid_info.x][grid_info.y].setOutStatus(grid_info.status)
        else:
            self.grid_board[grid_info.x][grid_info.y].setFlag(grid_info.flag)
            self.grid_board[grid_info.x][grid_info.y].setInStatus(grid_info.status)

        self.grid_board[grid_info.x][grid_info.y].render(screen)


class MineCount:
    def __init__(self, x, y, grid_size, total_count, back_color):
        self.x = x
        self.y = y
        self.grid_size = grid_size
        self.total_count = total_count
        self.back_color = back_color
        self.ipb_rect = pygame.Rect(self.x + self.grid_size * 1.5, self.y, self.grid_size, self.grid_size)
        self.ipb_ui = InputBox(self.ipb_rect, self.back_color, str(self.total_count))

    def render(self, screen):
        icon_rect = pygame.Rect(self.x, self.y, self.grid_size, self.grid_size)
        mc_ui = MineButton(icon_rect, self.grid_size, 15)
        mc_ui.render(screen)
        self.update(screen)

    def update(self, screen):
        self.ipb_ui.setTxt(str(self.total_count))
        self.ipb_ui.render(screen)

    def changeCount(self, val):
        self.total_count += val


class Box:
    def __init__(self, rect, back_color = pygame.Color("#00000F")):
        self.rect = rect
        self.back_color = back_color

    def render(self, screen):
        pygame.draw.rect(screen, self.back_color, self.rect)

    def getHead(self):
        return Location(self.rect.x, self.rect.y)


class EndBox:
    def __init__(self, mod, rect, back_color = pygame.Color("#238E23")):
        self.box = Box(rect, back_color)
        self.mod = mod
        self.rect = rect
        self.title_font = pygame.font.Font(None, 60)
        self.info_font = pygame.font.Font(None, 30)
        self.restart_button = Box(self.getButtonRect(), pygame.Color("#4A766E"))


    def getTitle(self):
        if self.mod:
            return "Victory!"
        return "Game Over!"

    def getTitleLocation(self):
        if self.mod:
            return (self.rect.x + self.rect.w/3,self.rect.y+self.rect.h/8)
        return (self.rect.x + self.rect.w/5,self.rect.y+self.rect.h/8)

    def getInfoLocation(self, num = 0):
        return (self.rect.x + self.rect.w / 6, self.rect.y + self.rect.h * (4 + num) / 8)

    def getButtonLocation(self):
        return (self.rect.x + self.rect.w * 3 / 7, self.rect.y+self.rect.h * 15 / 19)

    def getButtonRect(self):
        return (self.rect.x + self.rect.w / 4, self.rect.y+self.rect.h * 3 / 4, self.rect.w / 2,self.rect.h / 8)

    def beHit(self, x, y):
        if x > self.rect.x + self.rect.w / 4 and x < self.rect.x + self.rect.w * 3 / 4:
            if y > self.rect.y + self.rect.h * 3 / 4 and y < self.rect.y + self.rect.h * 7 / 8:
                return True
        return False

    def render(self, screen):
        self.box.render(screen)
        #title = pygame.draw.rect(screen, self.back_color, pygame.Rect(self.rect.x + self.rect.w/6,self.y+self.rect.h/8,self.rest.w*2/3,self.rect.h/4))
        txtSurface = self.title_font.render(self.getTitle(), True, pygame.Color("black"))
        screen.blit(txtSurface,self.getTitleLocation())
        txtSurface = self.info_font.render("Time: ", True, pygame.Color("black"))
        screen.blit(txtSurface, self.getInfoLocation())
        self.restart_button.render(screen)
        txtSurface = self.info_font.render("Restart", True, pygame.Color("black"))
        screen.blit(txtSurface, self.getButtonLocation())


# class GameTimer:
#     def __init__(self, x, y, grid_size):
#         self.x = x
#         self.y = y
#         self.grid_size = grid_size
#
#         ipb_rect = pygame.Rect(self.x + self.grid_size * 1.5, self.y, self.grid_size, self.grid_size)
#         self.ipb_ui = InputBox(ipb_rect, '0')
#
#     def render(self, screen):
#         icon_rect = pygame.Rect(self.x, self.y, self.grid_size, self.grid_size)
#         gt_ui = MineButton(icon_rect, self.grid_size, 14)
#         gt_ui.render(screen)
#
#         self.ipb_ui.render(screen)
#
#     def timeCount(self, screen):
#         current_time = pygame.time.get_ticks()
#         self.ipb_ui.resettxt(str(current_time))
#         self.ipb_ui.rendertxt(screen)
